package com.yike.user.service.impl;

import com.yike.common.exception.BusinessException;
import com.yike.common.tool.LoginUtil;
import com.yike.user.entity.PlatformMenu;
import com.yike.user.mapper.PlatformMenuMapper;
import com.yike.user.param.PlatformMenuParam;
import com.yike.user.service.PlatformMenuService;
import com.yike.user.param.PlatformMenuPageParam;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yike.common.base.BaseServiceImpl;
import com.yike.common.pagination.Paging;
import com.yike.common.pagination.PageInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yike.user.utils.PlatformMenuUtil;
import com.yike.user.vo.PlatformMenuTreeVo;
import com.yike.user.vo.PlatformMenuVo;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 平台菜单 服务实现类
 *
 * @author Sakura
 * @since 2024-10-29
 */
@Slf4j
@Service
public class PlatformMenuServiceImpl extends BaseServiceImpl<PlatformMenuMapper, PlatformMenu> implements PlatformMenuService {

    @Autowired
    private PlatformMenuMapper platformMenuMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean savePlatformMenu(PlatformMenuParam platformMenuParam) throws Exception {
        // 此处目前不做校验，直接保存即可
        PlatformMenu platformMenu = new PlatformMenu();
        BeanUtils.copyProperties(platformMenuParam, platformMenu);
        platformMenu.setCreateBy(LoginUtil.getUserId());
        return super.save(platformMenu);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updatePlatformMenu(PlatformMenuParam platformMenuParam) throws Exception {
        // 获取原信息
        PlatformMenu platformMenu = platformMenuMapper.selectById(platformMenuParam.getId());
        if (platformMenu == null) {
            throw new BusinessException("菜单信息异常");
        }
        BeanUtils.copyProperties(platformMenuParam, platformMenu);
        platformMenu.setUpdateBy(LoginUtil.getUserId());
        return super.updateById(platformMenu);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deletePlatformMenu(Long id) throws Exception {
        return super.removeById(id);
    }

    @Override
    public PlatformMenuVo getPlatformMenu(Long id) throws Exception {
        return platformMenuMapper.getPlatformMenuVo(id);
    }

    @Override
    public Paging<PlatformMenuVo> getPlatformMenuPageList(PlatformMenuPageParam platformMenuPageParam) throws Exception {
        Page<PlatformMenu> page = new PageInfo<>(platformMenuPageParam);
        IPage<PlatformMenuVo> iPage = platformMenuMapper.getPlatformMenuList(page, platformMenuPageParam);
        return new Paging<PlatformMenuVo>(iPage);
    }

    @Override
    public List<PlatformMenuTreeVo> tree() throws Exception {
        // 获取所有的权限菜单
        List<PlatformMenu> platformMenus = platformMenuMapper.getPlatformMenus(null);
        return PlatformMenuUtil.getPlatformMenuTreeVos(platformMenus);
    }

}
